package iiet.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import iiet.dao.FeeDetails;
import iiet.dao.FeeDetailsDAO;
import iiet.dao.FeeDetailsId;
import iiet.dao.Payments;
import iiet.dao.PaymentsDAO;
import iiet.dao.Student;
import iiet.dao.StudentDAO;
import iiet.dao.TypeFee;
import iiet.dao.TypeFeeDAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IietService {
	@Autowired
	PaymentsDAO paymentsDAO;
	@Autowired
	StudentDAO studentDAO;
	@Autowired
	FeeDetailsDAO feeDetailsDAO;
	@Autowired
	TypeFeeDAO typeFeeDAO;

	public String getMsg(String msg) {
		return "Hello " + msg;
	}

	public void savePayment(String receiptNumber, String htno, String year,
			Long tutionFee, Long admissionFee, Long hostelFee,
			Long transportFee, String paidBy) {

		Student student = studentDAO.findById(htno.toUpperCase());
		Set<FeeDetails> fset = (Set<FeeDetails>) student.getFeeDetailses();
		for (FeeDetails f : fset) {
			if (f.getId().getYearStudy().equals(year)) {
				f.setTutionFeePaid(f.getTutionFeePaid() + tutionFee);
				f.setAdmissionFeePaid(f.getAdmissionFeePaid() + admissionFee);
				f.setHostelFeePaid(f.getHostelFeePaid() + hostelFee);
				f.setTransportFeePaid(f.getTransportFeePaid() + transportFee);

				f.setTutionFeeDue(f.getTutionFee() - f.getTutionFeePaid());
				f.setAdmissionFeeDue(f.getAdmissionFee()
						- f.getAdmissionFeePaid());
				f.setHostelFeeDue(f.getHostelFee() - f.getHostelFeePaid());
				f.setTransportFeeDue(f.getTransportFee()
						- f.getTransportFeePaid());

				f.setFeePaid(f.getFeePaid()
						+ (tutionFee + admissionFee + hostelFee + transportFee));
				f.setFeeDue(f.getTotalFee() - f.getFeePaid());

			}
		}
		Payments payment = new Payments(receiptNumber, student, year,
				tutionFee, admissionFee, hostelFee, transportFee, paidBy);
		paymentsDAO.save(payment);
		// return true;
	}

	public void saveStudent(String htno, String name, String course,
			String branch, String year, String hostel, String bus,
			String category, long tutionFee, long admissionFee, long hostelFee,
			long transportFee, long totalFee, String fatherName, String mobile,
			String email, String parentMobile1, String parentMobile2,
			String address) {

		Student student = new Student(htno.toUpperCase(), name, course, branch.toUpperCase(), year,
				category, fatherName, mobile, email, parentMobile1,
				parentMobile2, address);
		
		FeeDetailsId feeDetailsId = new FeeDetailsId(student, year);
		FeeDetails feeDetails = feeDetailsDAO.findById(feeDetailsId);
		/*
		 * if(savedFeeDetails != null){ feePaid = savedFeeDetails.getFeePaid();
		 * feeDue = savedFeeDetails.getFeeDue();
		 * 
		 * } else { feePaid = 0; feeDue = totalFee; }
		 */
		if (feeDetails == null) {
			long tutionFeePaid = 0;
			long tutionFeeDue = tutionFee;
			long admissionFeePaid = 0;
			long admissionFeeDue = admissionFee;
			long hostelFeePaid = 0;
			long hostelFeeDue = hostelFee;
			long transportFeePaid = 0;
			long transportFeeDue = transportFee;
			long feePaid = 0; 
			long feeDue = totalFee;
			feeDetails = new FeeDetails(feeDetailsId, tutionFee,
					admissionFee, hostelFee, transportFee, tutionFeePaid,
					tutionFeeDue, admissionFeePaid, admissionFeeDue,
					hostelFeePaid, hostelFeeDue, transportFeePaid,
					transportFeeDue, totalFee, feePaid, feeDue);
		}
		else {
			feeDetails.setTutionFee(tutionFee);
			feeDetails.setAdmissionFee(admissionFee);
			feeDetails.setHostelFee(hostelFee);
			feeDetails.setTransportFee(transportFee);
			feeDetails.setTotalFee(totalFee);
			feeDetails.setFeeDue(feeDetails.getTotalFee() - feeDetails.getFeePaid());
			
		}
		/*
		 * FeeDetails feeDetails = new FeeDetails(feeDetailsId, tutionFee,
		 * admissionFee, hostelFee, transportFee, 0,0,0,0,0,0,0,0,totalFee,
		 * feePaid, feeDue);
		 */
		student.getFeeDetailses().add(feeDetails);
		studentDAO.save(student);
		// feeDetailsDAO.save(feeDetails);
		// return true;
	}

	public List findAllTypeFees() {
		return typeFeeDAO.findAll();

	}
	public List findAllDueStudents(String course, String branch, String year ) {
		
		
		Student student = new Student();
		
		
		student.setCourse(course);
		if(!branch.equalsIgnoreCase("all"))
			student.setBranch(branch);
		FeeDetails fd = new FeeDetails();
		fd.setId(new FeeDetailsId(student,year));
		//List<FeeDetails> flist= feeDetailsDAO.findByExample();
		
		
		if(!year.equalsIgnoreCase("all")) {
			/*FeeDetails feeDetails = new FeeDetails();
			feeDetails.setId(new FeeDetailsId(student, year));
			Set<FeeDetails> feeDetailses = new HashSet<FeeDetails>();
			feeDetailses.add(feeDetails);
			student.setFeeDetailses(feeDetailses);*/
			student.setYearStudy(year);
		}
		
		
		
		List<FeeDetails> flist = feeDetailsDAO.findByExample(fd); 
		List<Student> studentsList = studentDAO.findByExample(student);
		/*List<Student> dueStudentsList = new ArrayList<Student>();
		for(Student s: studentsList) {
			
		}*/
		return studentsList;
	}

}
